#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<queue>
#include<unordered_map>

using namespace std;

int numBusesToDestination(vector<vector<int>>& routes, int source, int target) {
    if (source == target)    return 0;

    int n = routes.size();
    vector<vector<int>> edge(n, vector<int>(n));
    unordered_map<int, vector<int>> rec;
    for (int i = 0; i < n; i++)
        for (int site : routes[i])
        {
            for (int j : rec[site])
                edge[i][j] = edge[j][i] = true;
            rec[site].push_back(i);
        }

    vector<int> dis(n, -1);
    queue<int> q;
    for (auto bus : rec[source])
    {
        dis[bus] = 1;
        q.push(bus);
    }
    while (q.size())
    {
        int x = q.front();
        q.pop();
        for (int y = 0; y < n; y++)
            if (edge[x][y] && dis[y] == -1)
            {
                dis[y] = dis[x] + 1;
                q.push(y);
            }
    }

    int res = INT_MAX;
    for (auto bus : rec[target])
        if (dis[bus] != -1)
            res = min(res, dis[bus]);
    return res == INT_MAX ? -1 : res;
}

int main()
{
    vector<vector<int>> vv = { {1,2,7 }, {3, 6, 7} };
    numBusesToDestination(vv, 1, 6);
    return 0;
}